#!/usr/bin/env python3.10
# -*- encoding: utf-8 -*-
"""
@File    :   controllers.py
@Time    :   2024/02/19 17:10:33
@Author  :   windstarry 
@Version :   1.0
"""
# here put the import lib
from ninja_extra import (
    ModelConfig,
    ModelControllerBase,
    ModelSchemaConfig,
    ModelPagination,
    api_controller,
    route
)
from ninja_jwt.authentication import JWTAuth
from ninja_extra.permissions import IsAuthenticated
from ninja_extra.schemas import NinjaPaginationResponseSchema
from ninja_extra.pagination import (
    PageNumberPaginationExtra,
    PaginatedResponseSchema,
    paginate,
)
from django.shortcuts import get_object_or_404
from core.schemas import ErrorSchema,MessageSchema
from .models import QuestionCategory,QuestionType
from .schemas import QuestionCategorySchema,CreateQuestionCategorySchema


@api_controller("/question", tags=["题目管理"],permissions=[IsAuthenticated], auth=JWTAuth())
class QuestionController:
    @route.get(
        "",
        summary="题目信息",
        url_name="question-info",
    )
    def get_question_info(
        self,
    ):
        return {"message": "这里是题目信息"}

    @route.get("/category", 
               summary="题目分类列表", 
               response=PaginatedResponseSchema[QuestionCategorySchema],
               url_name="question_category_list")
    @paginate(PageNumberPaginationExtra,page_size=10)
    def get_question_category_list(
        self,
    ):
        return QuestionCategory.objects.all()

    @route.post("/category", 
                summary="新增题目分类", 
                response=[(201, QuestionCategorySchema), (400, ErrorSchema)],
                url_name="add-question-category")
    def add_question_category(
        self,
        data:CreateQuestionCategorySchema,
    ):
        try:
            obj = data.create_obj(creator=self.context.request.user)
            return 201, obj
        except Exception as ex:
            return 400, {"message": "创建失败"}

    @route.get(
        "/category/{c_id}", 
        summary="获取题目分类",
        response= QuestionCategorySchema,
        url_name="get-question-category"
    )
    def get_question_category_by_id(self, c_id: int):
        obj = get_object_or_404(QuestionCategory,id=c_id)
        return obj


    @route.post(
        "/category/{c_id}", 
        summary="修改题目分类", 
        response= QuestionCategorySchema,
        url_name="update-question-category"
    )
    def update_question_category(self, c_id: int,data: CreateQuestionCategorySchema,):
        obj = get_object_or_404(QuestionCategory,id=c_id)
        obj.modifier = self.context.request.user.username
        data.update(obj)
        return obj

    @route.delete(
        "/category/{c_id}", 
        summary="删除题目分类", 
        response={200:MessageSchema},
        url_name="delete-question-category"
    )
    def delete_question_category(self, c_id: int):
        obj =  get_object_or_404(QuestionCategory,id=c_id)
        obj.delete()
        return 200,{"message": "删除成功"}
    
    # @route.get("/type", summary="题目类型列表", url_name="question-type")
    # def get_question_type_list(
    #     self,
    # ):
    #     return {"message": "这里是题目类型列表"}

    # @route.post("/type", summary="新增题目类型", url_name="add-question-type")
    # def add_question_type(
    #     self,
    # ):
    #     return {"message": "这里是新增题目类型"}

    # @route.get("/type/{t_id}", summary="获取题目类型", url_name="get-question-type")
    # def get_question_type_by_id(self, t_id: int):
    #     return {"message": "这里是通过id获取题目类型"}

    # @route.post("/type/{t_id}", summary="修改题目类型", url_name="update-question-type")
    # def update_question_type(self, t_id: int):
    #     return {"message": "这里是修改题目类型"}

    # @route.delete(
    #     "/type/{t_id}", summary="删除题目类型", url_name="delete-question-type"
    # )
    # def delete_question_type(self, t_id: int):
    #     return {"message": "这里是删除题目类型"}

    
    
@api_controller("/question/type", tags=["题目类型管理"],permissions=[IsAuthenticated], auth=JWTAuth())
class QuestionTypeController(ModelControllerBase):
    model_config = ModelConfig(
        model=QuestionType,
        schema_config=ModelSchemaConfig(read_only_fields=["id",]),
        pagination=ModelPagination(
            pagination_schema=NinjaPaginationResponseSchema
        ),
    )
    